/******************************************************************************************
  This is to plot the moments and profiles from baseline model and other variation of models
  in the same graph --- exogenous, learning-by-doing

  in their own folder, 
  ** dta_MSM_Profiles.dta: profiles

  ** dta_MSM_Moments.dta: moments
        _f: generate from Fortram program
        _d: from SIPP data
        _indv: generated in Stata based on simulated data.
*******************************************************************************************/

*by Xiaodong Fan, fanxiaodong@gmail.com

#delimit ;

cap log close;
clear all;
drop _all;
set more 1;
pause on;

log using log_MSM_alt_compare.log, replace;

global gpicsdir "pics_alt_compare";
global gdtadir ".";

include do_globals.do;


********************************************************************;
*  Moments from baseline and various models;
* model: 0-baseline exogenous learning by doing
********************************************************************;

* the data moments;
use ${gdtadir}/dta_sipp_Moments.dta;
gen byte model = 99;
cap ren *_d *;
sort t;
save ${gdtadir}/dta_MSM_Profiles_alt_compare.dta, replace;

* from the baseline model;
use ${gdtadir}/dta_MSM_Profiles.dta, clear;
keep if ihet==0;
drop ihet;
gen byte model = 0;
cap ren *_f *;
append using ${gdtadir}/dta_MSM_Profiles_alt_compare.dta;
sort model t;
save ${gdtadir}/dta_MSM_Profiles_alt_compare.dta, replace;

* from the exogenous and learning by doing model;
foreach iv in 1 2 {;
   use ../../../BP_MPI_${gv_alt`iv'}/main/output/dta_MSM_Profiles.dta, clear;
   keep if ihet==0;
   drop ihet;
   gen byte model = ${gv_alt`iv'_id};
   cap ren *_f *;
   append using ${gdtadir}/dta_MSM_Profiles_alt_compare.dta;
   sort model t;
   save ${gdtadir}/dta_MSM_Profiles_alt_compare.dta, replace;
};


use ${gdtadir}/dta_MSM_Profiles_alt_compare.dta, clear;

ren V0 V_work;
ren V1 V_notwork;

local lvprofiles A H AIME C I I_cond L labor lfpr olabor inc lnw lnw_fd irecss
                 EV V_work V_notwork lfpr_diff* sdlnw ssa nn plnw41
                 C_lfpr0 C_lfpr1 alfpr0to1 alfpr1to0 lfpr0to1 lfpr1to0 probV0 FS_spinc iFS;

cap drop col;

keep `lvprofiles' t model;

reshape wide `lvprofiles', i(t) j(model);

sort t;

*save ${gdtadir}/dta_MSM_Profiles_alt_compare.dta, replace;

* plot graph;
local lvmoments lfpr lnw sdlnw lnw_fd C ssa;

foreach iv in `lvmoments' lfpr2 {;
   local lyange "";
   local lylabel "";

   if ("`iv'"=="lfpr2") {;
       local lvx lfpr;
       local lvtperiod "if t<=70";
       local lvxlabel "18 ${gvdata0} 30(10)60 ${gvdata10}";
   };
   else if  ("`iv'"=="ssa") {;
       local lvx ssa;
       local lvtperiod "if t>=${gvdata_ss} & t<=${gvdata10_ss}";
       local lvxlabel "${gvdata_ss}(1)${gvdata10_ss}";
   };
   else {;
      local lvx `iv';
      local lvtperiod "if t>=${gvdata0} & t<=${gvdata10}";
      local lvxlabel "${gvdata0} 30(10)60 ${gvdata10}";
   };

   if ("`iv'"=="C") {;
      local lvyrange "6 12";
      local lvylabel "6(2)12";
   };
   else {;
      local lvyrange ${gyrange_`lvx'};
      local lvylabel ${gylabel_`lvx'};
   };

   line `lvx'99 `lvx'0 `lvx'${gv_alt1_id} `lvx'${gv_alt2_id} t `lvtperiod', 
        lpattern("l" "-..." "_" "_..") graphregion(color(white))
        xlabel(`lvxlabel',grid) 
        ytitle("${gytitle_`lvx'}") yscale(range(`lvyrange')) ylabel(`lvylabel')
        xline(${gvdata0}, lcolor(gs15) lwidth(medthick))
        legend(pos(${gpos_`iv'}) ring(0) col(1) order(1 "Data" 2 "Baseline" 3 "${gv_alt1_des}" 4 "${gv_alt2_des}" ));
   graph export ${gpicsdir}/eps_moments_compare_`iv'.eps,replace;
};


cap log close;



